package com.wakey.rtcs.web.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wakey.rtcs.miniui.PageManager;
import com.wakey.rtcs.web.model.User;
import com.wakey.rtcs.web.service.UserService;

import net.sf.json.JSONObject;


@Controller
@RequestMapping("/user") // url:/模块/资源/{id}/细分 /seckill/list
public class UserController {

	@Autowired
	private UserService userService;
	
	
	@RequestMapping(value = "/queryUserList")
	@ResponseBody
	private PageManager<User> queryUserList(HttpServletRequest request,PageManager<User> pager,User user) {
		pager.setData(userService.findAll(user));
		return pager;
	}
	
	@RequestMapping(value = "/queryById")
	@ResponseBody
	private User queryById(HttpServletRequest request,Long id) {
		return userService.queryById(id);
	}
	
	@RequestMapping(value = "/deleteUser", method = RequestMethod.GET)
	@ResponseBody
	private boolean deleteUser(HttpServletRequest request,String ids) {
		int num = userService.deleteUser(ids.split(","));	
		return num>0?true:false;
	}
	
	@RequestMapping(value = "/editUser")
	@ResponseBody
	private boolean editUser(HttpServletRequest request,User user) {
		int num = userService.editUser(user);	
		return num>0?true:false;
	}
	
	/**
	 * 网页登录
	 * @param userName
	 * @param userPwd
	 * @return
	 */
	@RequestMapping(value = "/userLoginWeb", method = RequestMethod.POST)
	@ResponseBody
	private String userLoginWeb(HttpServletRequest request,Model model) {
		
		String userName = request.getParameter("userName");
		String userPwd = request.getParameter("userPwd");
		
		User user = userService.getUserByLogin(userName, userPwd);
		
		JSONObject jsoTotal = new JSONObject();
		jsoTotal.put("msg", "");
		jsoTotal.put("count", 1);
		


		if(user != null) {
			request.getSession().setAttribute("userName", user.getName());
			request.getSession().setAttribute("userType", user.getType());
			request.getSession().setAttribute("userId", user.getId());
			request.getSession().setAttribute("nickName", user.getNickName());
			if(user.getIsNew() != 1) {
				jsoTotal.put("code", 1);
				jsoTotal.put("url", "http://localhost:8080/document/views/modifyPassword.jsp");
				userService.makeOld(user.getId());
				return jsoTotal.toString();
			}
			jsoTotal.put("code", 1);
			jsoTotal.put("url", "http://localhost:8080/document/");

			JSONObject jso = new JSONObject();
			jsoTotal.put("code", 1);
			jso.put("userId", user.getId());
			jso.put("userName", user.getName());
			jsoTotal.put("data", jso);
		}else {
			jsoTotal.put("code", 0);
			jsoTotal.put("data", new JSONObject());
		}
//		return "/parts/index";
		
		return jsoTotal.toString();
	}
	
	@RequestMapping(value = "/userRegister", method = RequestMethod.POST)
	@ResponseBody
	private boolean userRegister(HttpServletRequest request,String email,String nickName) {
		
		String userName = request.getParameter("userName");
		String userPassword = request.getParameter("userPwd");
		int userType = Integer.parseInt(request.getParameter("userType"));
		
		int num = userService.addUser(userName, userPassword,userType,email,nickName);
		
		return num>0?true:false;
	}
	
	@RequestMapping(value = "/addUser", method = RequestMethod.POST)
	@ResponseBody
	private boolean addUser(HttpServletRequest request,User user) {
		
		int num = userService.addUserObj(user);
		
		return num>0?true:false;
	}
	@RequestMapping(value = "/updateUser", method = RequestMethod.POST)
	@ResponseBody
	private boolean updateUser(HttpServletRequest request) {
		
		Long id = Long.parseLong(request.getParameter("userId"));
		String password = request.getParameter("userPwd");
		
		int num = userService.updateUser(id, password);
		
		return num>0?true:false;
	}
	
	@RequestMapping(value = "/forgetPassword", method = RequestMethod.POST)
	@ResponseBody
	private boolean forgetPassword(HttpServletRequest request,String password,String email,String userName) {
		
		
		int num = userService.forgetPassword(userName, email, password);
		
		return num>0?true:false;
	}
	
	
	/**
	 * 登录
	 * @param userName
	 * @param userPwd
	 * @return
	 */
	@RequestMapping(value = "/api/userLogin", method = RequestMethod.POST, produces = {
	"application/json; charset=utf-8" })
	@ResponseBody
	public String userLogin(@RequestBody String param) {
		
		JSONObject jsStr = JSONObject.fromObject(param);
		String userName = jsStr.getString("userName");
		String userPwd = jsStr.getString("userPwd");
		
		User user = userService.getUserByLogin(userName, userPwd);
		
		JSONObject jsoTotal = new JSONObject();
		jsoTotal.put("msg", "");
		jsoTotal.put("count", 1);
		
		if(user != null) {

			jsoTotal.put("code", 1);

			JSONObject jso = new JSONObject();
			jso.put("userId", user.getId());
			jso.put("userName", user.getName());
			jsoTotal.put("data", jso);
		}else {

			jsoTotal.put("code", 0);
			jsoTotal.put("data", new JSONObject());
		}
		
		return jsoTotal.toString();
	}
	
	private String date2String(Date date) {
		if(date != null)
			
			return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);  
		else
			return "";
	}
	
	/**
	 * 注册
	 * @param userName
	 * @param userPwd
	 * @return
	 */
	@RequestMapping(value = "/api/userRegist", method = RequestMethod.POST, produces = {
	"application/json; charset=utf-8" })
	@ResponseBody
	private String userRegistor(@RequestBody String param) {
		
		String userId = UUID.randomUUID().toString();
		JSONObject jsStr = JSONObject.fromObject(param);
		String userName = jsStr.getString("userName");
		String userPwd = jsStr.getString("userPwd");
		
		Integer x = userService.insertUser(userId, userName, userPwd);
		
		JSONObject jsoTotal = new JSONObject();
		jsoTotal.put("code", x); // 1是成功 其他失败
		jsoTotal.put("msg", "");
		jsoTotal.put("count", 1);
		jsoTotal.put("data", new JSONObject());
		
		return jsoTotal.toString();
	}
	
}